﻿package com.mui.controls
{
    import __AS3__.vec.*;
    import com.gengine.core.*;
    import com.gengine.global.*;
    import com.mui.utils.*;
    import fl.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.text.*;

    public class GAlertWin extends GSprite
    {
        protected var title:DisplayObject;
        protected var message:GTextFiled;
        protected var background:Sprite;
        protected var prompt:Sprite;
        protected var btnSp:Sprite;
        protected var boarder:Sprite;
        protected var allButtons:Vector.<GButton>;
        protected var icon:DisplayObject;
        protected var boarderWidth:int = 15;
        protected var minWidth:int = 230;
        protected var minHeight:int = 150;
        protected var _stageWidth:Number = 1;
        protected var _stageHeight:Number = 1;
        protected var closeHandler:Function;
        protected var closeBtn:GButton;
        protected var extendObj:Object;
        public var w:Number = -1;
        public var h:Number = -1;

        public function GAlertWin(param1:Number, param2:Number) : void
        {
            this.allButtons = new Vector.<GButton>;
            this._stageWidth = param1;
            this._stageHeight = param2;
            this.isHideDispose = true;
            return;
        }// end function

        public function set stageWidth(param1:Number) : void
        {
            this._stageWidth = param1;
            return;
        }// end function

        public function set stageHeight(param1:Number) : void
        {
            this._stageHeight = param1;
            return;
        }// end function

        protected function onCloseBtnClickHandler(event:Event) : void
        {
            if (this.closeHandler != null)
            {
                if (this.extendObj != null)
                {
                    this.closeHandler(Alert.CLOSE, this.extendObj);
                }
                else
                {
                    this.closeHandler(Alert.CLOSE);
                }
            }
            this.parent.removeChild(this);
            return;
        }// end function

        protected function createChildren() : void
        {
            if (Alert.title is DisplayObject)
            {
                this.title = Alert.title;
            }
            else
            {
                this.title = this.createTextField();
                if (Alert.titleTextFormat)
                {
                    (this.title as GTextFiled).defaultTextFormat = Alert.titleTextFormat;
                }
            }
            this.message = this.createMessgeField();
            if (Alert.messageTextFormat)
            {
            }
            this.background = this.createBackground();
            this.boarder = this.createPromptBoarder();
            this.allButtons = this.createButtons();
            if (Alert.iconClass)
            {
                this.icon = new Alert.iconClass();
            }
            if (Alert.showCloseBtn)
            {
                this.closeBtn = this.createCloseBtn();
                this.closeBtn.configEventListener(MouseEvent.CLICK, this.onCloseBtnClickHandler);
            }
            return;
        }// end function

        protected function addBtns() : void
        {
            var _loc_3:GButton = null;
            var _loc_1:* = this.allButtons.length;
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = this.allButtons[_loc_2];
                _loc_3.configEventListener(MouseEvent.CLICK, this.buttonClickHandler);
                _loc_3.x = _loc_2 * (Alert.buttonWidth + Alert.buttonGap);
                this.btnSp.addChild(_loc_3);
                _loc_2++;
            }
            return;
        }// end function

        protected function setTitle() : void
        {
            return;
        }// end function

        protected function setMessage() : void
        {
            this.message.htmlText = Alert.text;
            return;
        }// end function

        protected function writeData() : void
        {
            this.setTitle();
            this.setMessage();
            this.addBtns();
            return;
        }// end function

        protected function get myWidth() : Number
        {
            var _loc_1:int = 0;
            if (this.w == -1)
            {
                _loc_1 = this.boarderWidth * 2;
                this.w = Math.max(this.title.width + _loc_1, this.message.width + _loc_1, this.btnSp.width + _loc_1, this.minWidth);
            }
            return this.w;
        }// end function

        protected function get myHeight() : Number
        {
            if (this.h == -1)
            {
                this.h = Math.max(this.title.height + 5, 15) + this.message.height + Alert.buttonHeight + 45;
            }
            return this.h;
        }// end function

        protected function setBoarderSize() : void
        {
            this.boarder.width = this.myWidth;
            this.boarder.height = this.myHeight;
            return;
        }// end function

        protected function layoutMessage() : void
        {
            this.message.x = this.myWidth / 2 - this.message.width / 2;
            this.message.y = Math.max(this.title.height + 15, 15);
            return;
        }// end function

        protected function layoutBtnSp() : void
        {
            this.btnSp.x = (this.myWidth - (Alert.buttonWidth + Alert.buttonGap) * this.allButtons.length + Alert.buttonGap) / 2;
            this.btnSp.y = this.message.y + this.message.height + 15;
            return;
        }// end function

        protected function layoutPrompt() : void
        {
            var _loc_1:GButton = null;
            if (Alert.isFlowMouse)
            {
                _loc_1 = this.getButtonByFlag(Alert.OK);
                if (_loc_1 == null)
                {
                    _loc_1 = this.getButtonByFlag(Alert.YES);
                }
                this.prompt.x = Global.stage.mouseX - (this.btnSp.x + _loc_1.x + _loc_1.width / 2);
                this.prompt.y = Global.stage.mouseY - (this.btnSp.y + _loc_1.x + _loc_1.height / 2);
                if (this.prompt.x + this.prompt.width > Global.stage.stageWidth)
                {
                    this.prompt.x = Global.stage.stageWidth - this.prompt.width;
                }
                if (this.prompt.y + this.prompt.height > Global.stage.stageHeight)
                {
                    this.prompt.y = Global.stage.stageHeight - this.prompt.height;
                }
            }
            else
            {
                this.prompt.x = (this._stageWidth - this.prompt.width) / 2;
                this.prompt.y = (this._stageHeight - this.prompt.height) / 2;
            }
            return;
        }// end function

        protected function layoutCloseBtn() : void
        {
            if (Alert.showCloseBtn)
            {
                this.closeBtn.x = this.myWidth - this.closeBtn.width;
                this.closeBtn.y = 10;
                this.prompt.addChild(this.closeBtn);
            }
            return;
        }// end function

        protected function layoutTitle() : void
        {
            return;
        }// end function

        protected function layoutAllUI() : void
        {
            this.setBoarderSize();
            this.layoutMessage();
            this.layoutBtnSp();
            this.layoutTitle();
            this.addChild(this.background);
            this.addChild(this.prompt);
            this.prompt.addChild(this.boarder);
            this.prompt.addChild(this.title);
            this.prompt.addChild(this.message);
            this.layoutCloseBtn();
            if (this.icon)
            {
                addChild(this.icon);
            }
            this.prompt.addChild(this.btnSp);
            this.layoutPrompt();
            return;
        }// end function

        protected function createPromptBoarder() : Sprite
        {
            var _loc_1:* = UICompomentPool.getUICompoment(Sprite) as Sprite;
            var _loc_2:int = 10;
            _loc_1.graphics.lineStyle(1);
            _loc_1.graphics.beginFill(Alert.backgroundColor);
            _loc_1.graphics.drawRoundRect(0, 0, 100, 100, _loc_2, _loc_2);
            _loc_1.graphics.endFill();
            _loc_1.scale9Grid = new Rectangle(20, 20, 60, 60);
            _loc_1.filters = [this.getGlowFilter(), this.getDropShadowFilter()];
            _loc_1.alpha = Alert.backgroundAlpha;
            return _loc_1;
        }// end function

        protected function createButtons() : Vector.<GButton>
        {
            var _loc_6:GButton = null;
            var _loc_1:Array = [Alert.yesLabel, Alert.noLabel, Alert.okLabel, Alert.calcelLabel];
            var _loc_2:Array = [Alert.YES, Alert.NO, Alert.OK, Alert.CANCEL];
            var _loc_3:* = this.getButtonFlagsList();
            var _loc_4:* = _loc_3.length;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_4)
            {
                
                if (_loc_3[_loc_5] == 1)
                {
                    _loc_6 = UICompomentPool.getUICompoment(GButton) as GButton;
                    _loc_6.styleName = Alert.buttonStyleName ? (Alert.buttonStyleName) : ("ButtonNew");
                    _loc_6.label = _loc_1[_loc_5];
                    _loc_6.name = "btn_" + _loc_2[_loc_5];
                    _loc_6.setSize(Alert.buttonWidth, Alert.buttonHeight);
                    this.allButtons.push(_loc_6);
                }
                _loc_5++;
            }
            return this.allButtons;
        }// end function

        protected function createCloseBtn() : GButton
        {
            var _loc_1:* = UICompomentPool.getUICompoment(GLoadedButton) as GButton;
            _loc_1.label = "";
            _loc_1.setSize(22, 20);
            _loc_1.styleName = "Close_upSkin";
            return _loc_1;
        }// end function

        protected function createMessgeField() : GTextFiled
        {
            var _loc_1:* = this.createTextField(this.minWidth - this.boarderWidth * 2);
            _loc_1.wordWrap = Alert.wordWrap;
            return _loc_1;
        }// end function

        protected function createTextField(param1:int = 0, param2:int = 0) : GTextFiled
        {
            var _loc_3:* = new GTextFiled();
            if (param1 > 0)
            {
                _loc_3.width = param1;
            }
            if (param2 > 0)
            {
                _loc_3.height = param2;
            }
            _loc_3.textColor = Alert.textColor;
            _loc_3.multiline = true;
            _loc_3.selectable = false;
            _loc_3.mouseEnabled = false;
            _loc_3.autoSize = TextFieldAutoSize.LEFT;
            return _loc_3;
        }// end function

        protected function createBackground() : Sprite
        {
            var _loc_2:int = 0;
            var _loc_3:BitmapData = null;
            var _loc_4:BitmapData = null;
            var _loc_5:Rectangle = null;
            var _loc_6:Point = null;
            var _loc_7:uint = 0;
            var _loc_8:GBitmap = null;
            var _loc_1:* = UICompomentPool.getUICompoment(Sprite) as Sprite;
            _loc_2 = Alert.backgroundColor;
            switch(Alert.mode)
            {
                case Alert.Mode_Blur:
                {
                    _loc_3 = new BitmapData(this._stageWidth, this._stageHeight, true, 4278190080 + _loc_2);
                    _loc_4 = new BitmapData(this._stageWidth, this._stageHeight);
                    _loc_4.draw(stage);
                    _loc_5 = new Rectangle(0, 0, this._stageWidth, this._stageHeight);
                    _loc_6 = new Point(0, 0);
                    _loc_7 = 120;
                    _loc_3.merge(_loc_4, _loc_5, _loc_6, _loc_7, _loc_7, _loc_7, _loc_7);
                    _loc_3.applyFilter(_loc_3, _loc_5, _loc_6, new BlurFilter(5, 5));
                    _loc_8 = UICompomentPool.getUICompoment(GBitmap) as GBitmap;
                    _loc_8.bitmapData = _loc_3;
                    _loc_1.addChild(_loc_8);
                    _loc_1.mouseEnabled = true;
                    break;
                }
                case Alert.Mode_None:
                {
                    _loc_1.graphics.beginFill(_loc_2, 0);
                    _loc_1.graphics.drawRect(0, 0, this._stageWidth, this._stageHeight);
                    _loc_1.graphics.endFill();
                    _loc_1.mouseEnabled = true;
                    break;
                }
                case Alert.Mode_NoneNotModal:
                {
                    _loc_1.graphics.beginFill(_loc_2, 0);
                    _loc_1.graphics.drawRect(0, 0, this._stageWidth, this._stageHeight);
                    _loc_1.graphics.endFill();
                    _loc_1.mouseEnabled = false;
                    break;
                }
                case Alert.Mode_Simple:
                {
                    _loc_1.graphics.beginFill(_loc_2, Alert.backgroundAlpha);
                    _loc_1.graphics.drawRect(0, 0, this._stageWidth, this._stageHeight);
                    _loc_1.graphics.endFill();
                    _loc_1.mouseEnabled = true;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return _loc_1;
        }// end function

        protected function getButtonFlagsList() : Array
        {
            var _loc_1:Array = null;
            if (Alert.buttonFlags != 0)
            {
                _loc_1 = ByteToArray(Alert.buttonFlags);
            }
            else
            {
                _loc_1 = ByteToArray(Alert.defaultButtonFlag);
            }
            return _loc_1;
        }// end function

        protected function doStartDrag(event:MouseEvent) : void
        {
            if (event.target is Sprite && !(event.target is Button))
            {
                event.currentTarget.startDrag();
                this.prompt.addEventListener(MouseEvent.MOUSE_UP, this.doStopDrag);
            }
            return;
        }// end function

        protected function doStopDrag(event:MouseEvent) : void
        {
            if (event.target is Sprite)
            {
                event.currentTarget.stopDrag();
                this.prompt.removeEventListener(MouseEvent.MOUSE_UP, this.doStopDrag);
            }
            return;
        }// end function

        protected function RGBToHex(param1:int, param2:int, param3:int) : int
        {
            var _loc_4:uint = 0;
            _loc_4 = param1 & 255 << 16;
            _loc_4 = _loc_4 + (param2 & 255 << 8);
            _loc_4 = _loc_4 + (param3 & 255);
            return _loc_4;
        }// end function

        protected function keepInBounds(param1:int) : int
        {
            if (param1 < 0)
            {
                param1 = 0;
            }
            if (param1 > 255)
            {
                param1 = 255;
            }
            return param1;
        }// end function

        protected function buttonClickHandler(event:MouseEvent) : void
        {
            if (this.closeHandler != null)
            {
                if (this.extendObj != null)
                {
                    this.closeHandler(Number(event.target.name.substr(4)), this.extendObj);
                }
                else
                {
                    this.closeHandler(Number(event.target.name.substr(4)));
                }
            }
            this.parent.removeChild(this);
            return;
        }// end function

        protected function onResize(event:Event) : void
        {
            if (this.background)
            {
                if (this.stage)
                {
                    this.background.width = this.stage.stageWidth;
                    this.background.height = this.stage.stageHeight;
                }
            }
            return;
        }// end function

        public function get window() : DisplayObjectContainer
        {
            return this.prompt;
        }// end function

        public function get promptBackground() : DisplayObject
        {
            return this.boarder;
        }// end function

        protected function getDropShadowFilter() : DropShadowFilter
        {
            var _loc_1:Number = 0;
            var _loc_2:Number = 90;
            var _loc_3:Number = 0.6;
            var _loc_4:Number = 12;
            var _loc_5:Number = 4;
            var _loc_6:Number = 1;
            var _loc_7:Number = 1;
            var _loc_8:Boolean = false;
            var _loc_9:Boolean = false;
            var _loc_10:* = BitmapFilterQuality.LOW;
            return new DropShadowFilter(_loc_6, _loc_2, _loc_1, _loc_3, _loc_4, _loc_5, _loc_7, _loc_10, _loc_8, _loc_9);
        }// end function

        protected function getGlowFilter() : GlowFilter
        {
            var _loc_1:Number = 16777215;
            var _loc_2:Number = 0.8;
            var _loc_3:Number = 15;
            var _loc_4:Number = 15;
            var _loc_5:Number = 0.7;
            var _loc_6:Boolean = true;
            var _loc_7:Boolean = false;
            var _loc_8:* = BitmapFilterQuality.HIGH;
            return new GlowFilter(_loc_1, _loc_2, _loc_3, _loc_4, _loc_5, _loc_8, _loc_6, _loc_7);
        }// end function

        public function getButtonByFlag(param1:int) : GButton
        {
            var _loc_2:GButton = null;
            for each (_loc_2 in this.allButtons)
            {
                
                if (_loc_2.name == "btn_" + param1)
                {
                    return _loc_2;
                }
            }
            return null;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.extendObj = Alert.extendObj;
            this.closeHandler = Alert.closeHandler;
            this.prompt = UICompomentPool.getUICompoment(Sprite) as Sprite;
            this.btnSp = UICompomentPool.getUICompoment(Sprite) as Sprite;
            this.createChildren();
            this.writeData();
            this.layoutAllUI();
            this.prompt.addEventListener(MouseEvent.MOUSE_DOWN, this.doStartDrag);
            this.addEventListener(Event.CLOSE, this.onCloseBtnClickHandler);
            return;
        }// end function

        override protected function onAddToStageCreate(event:Event) : void
        {
            super.onAddToStageCreate(event);
            this.stage.addEventListener(Event.RESIZE, this.onResize);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this.prompt.removeEventListener(MouseEvent.MOUSE_DOWN, this.doStartDrag);
            this.stage.removeEventListener(Event.RESIZE, this.onResize);
            this.removeEventListener(Event.CLOSE, this.onCloseBtnClickHandler);
            this.w = -1;
            this.h = -1;
            this.allButtons.splice(0, this.allButtons.length);
            this.disposeUI(this.icon);
            this.disposeUI(this.title);
            this.disposeUI(this.message);
            this.disposeUI(this.closeBtn);
            this.disposeSprite(this.background);
            this.disposeSprite(this.prompt);
            this.disposeSprite(this.boarder);
            this.disposeSprite(this.btnSp);
            this.icon = null;
            this.title = null;
            this.message = null;
            this.closeBtn = null;
            this.background = null;
            this.prompt = null;
            this.boarder = null;
            this.btnSp = null;
            UICompomentPool.disposeObject(this);
            return;
        }// end function

        private function disposeUI(param1:DisplayObject) : void
        {
            if (param1 && param1 is IDispose)
            {
                IDispose(param1).dispose(true);
            }
            else if (param1)
            {
                param1.parent.removeChild(param1);
            }
            return;
        }// end function

        private function disposeSprite(param1:Sprite) : void
        {
            var _loc_4:DisplayObject = null;
            var _loc_2:* = param1.numChildren;
            var _loc_3:* = _loc_2 - 1;
            while (_loc_3 >= 0)
            {
                
                _loc_4 = param1.getChildAt(_loc_3);
                param1.removeChildAt(_loc_3);
                if (_loc_4 is IDispose)
                {
                    (_loc_4 as IDispose).dispose();
                }
                _loc_3 = _loc_3 - 1;
            }
            if (param1.parent)
            {
                param1.parent.removeChild(param1);
            }
            UICompomentPool.disposeUICompoment(param1);
            return;
        }// end function

        static function ByteToArray(param1:uint) : Array
        {
            var _loc_2:* = new Array();
            _loc_2.push(param1 & 1);
            _loc_2.push((param1 & 2) >> 1);
            _loc_2.push((param1 & 4) >> 2);
            _loc_2.push((param1 & 8) >> 3);
            return _loc_2;
        }// end function

        static function hexToRGB(param1:uint) : Array
        {
            var _loc_2:* = new Array();
            _loc_2.push(param1 >> 16);
            var _loc_3:* = param1 ^ _loc_2[0] << 16;
            _loc_2.push(_loc_3 >> 8);
            _loc_2.push(_loc_3 ^ _loc_2[1] << 8);
            return _loc_2;
        }// end function

    }
}
